package mulMan.gui.view;

import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;

import mulMan.domain.ListOfPlaylists;
import mulMan.domain.Playlist;

public class PlaylistsFullView extends JFrame implements Observer {

	private static final long serialVersionUID = 4874267953096856903L;
	
	private JLayeredPane panel;
	private JMenuItem menuItemAddNewAudioFile;
	private JMenuItem menuItemAddNewPlaylist;
	private JMenuItem menuItemExit;
	private JPanel audioFilesView = null;
	private List<Playlist> playlists = null;
	@SuppressWarnings("rawtypes")
	private JList listOfPlaylists;
	private JPopupMenu popupMenu;
	private JMenuItem menuItemDelete;
	
	
	
	
	public JPopupMenu getPopupMenu() {
		return popupMenu;
	}
	public void setPopupMenu(JPopupMenu popupMenu) {
		this.popupMenu = popupMenu;
	}
	@SuppressWarnings("rawtypes")
	public JList getListOfPlaylists() {
		return listOfPlaylists;
	}
	@SuppressWarnings("rawtypes")
	public void setListOfPlaylists(JList listOfPlaylists) {
		this.listOfPlaylists = listOfPlaylists;
	}
	public JPanel getAudioFilesView() {
		return audioFilesView;
	}
	public void setAudioFilesView(JPanel audioFilesView) {
		if (!this.isVisible()) {
			setVisible(true);
		} else {
			panel.remove(this.audioFilesView);
		}
		
		this.audioFilesView = audioFilesView;
		this.audioFilesView.setBounds(180, 5, 410, 340);
		
		panel.add(this.audioFilesView);
	}
	
	
	public PlaylistsFullView(List<Playlist> playlists) {
		this.playlists = playlists;
		
		((ListOfPlaylists)playlists).addObserver(this);
		for (Playlist iterator : playlists) {
			iterator.addObserver(this);
		}
		
		initView();
	    initFields();
	    setPopup();
	}

	private void setPopup() {
		menuItemDelete = new JMenuItem("Remove Playlist");
		popupMenu = new JPopupMenu();
		popupMenu.add(menuItemDelete);
	}
	
	public void update(Observable o, Object arg) {
		if (arg == "list") {
			((AudioFilesView)audioFilesView).resetList();
		}
		else if ((o instanceof ListOfPlaylists) && (arg != "name")) {
			resetList();
		}
	}

	@SuppressWarnings("unchecked")
	private void resetList() {
		listOfPlaylists.setModel(new PlaylistListModel(playlists));
	}
	
	public void initView() {
		panel = new JLayeredPane();
	    panel.setLayout(null);
	    getContentPane().add(panel);
	    
		setTitle("Playlist window");
	    setSize(600, 400);
	    setResizable(false);
	    setLocationRelativeTo(null);
	    setDefaultCloseOperation(EXIT_ON_CLOSE);
	    
	    initLibraryList();
	    initMenu();
	    
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initLibraryList() {
		PlaylistListModel playlistsListModel = new PlaylistListModel(playlists);
		
		listOfPlaylists = new JList(playlistsListModel);
	    listOfPlaylists.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	    listOfPlaylists.setLayoutOrientation(JList.VERTICAL);
	    
	    JScrollPane listScroller = new JScrollPane(listOfPlaylists);
	    listScroller.setSize(170, 340);
	    
	    JPanel p = new JPanel();
	    p.setBounds(5, 5, 170, 340);
	    p.setLayout(null);
	    
	    p.add(listScroller);
	    panel.add(p);
	}
	private void initMenu() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu menuFile = new JMenu("File");
		menuBar.add(menuFile);
		
		menuItemAddNewAudioFile = new JMenuItem("Add New Audio File");
		menuFile.add(menuItemAddNewAudioFile);
		
		menuItemAddNewPlaylist = new JMenuItem("Add New Playlist");
		menuFile.add(menuItemAddNewPlaylist);
		
		menuItemExit = new JMenuItem("Exit");
		menuFile.add(menuItemExit);
		
		setJMenuBar(menuBar);
	}
	
	public void initFields() {
	}
	
	
	public void addAddNewAudioFileListener(ActionListener anafl) {
		menuItemAddNewAudioFile.addActionListener(anafl);
    }
	public void addAddNewPlaylistListener(ActionListener anpl) {
		menuItemAddNewPlaylist.addActionListener(anpl);
    }
	public void addExitListener(ActionListener el) {
		menuItemExit.addActionListener(el);
    }
	public void addListPlaylistsListener(ListSelectionListener lpl) {
		listOfPlaylists.addListSelectionListener(lpl);
    }
	public void addListPlaylistsMouseListener(MouseListener lpml) {
		listOfPlaylists.addMouseListener(lpml);
    }
	
	public void adddeletePlaylistListener(ActionListener dpl) {
		menuItemDelete.addActionListener(dpl);
    }
}
